const Router = require('koa-router');
// 整数校验器
const {PositiveIntegerValidator, ClassicValidator} = require('../../validators/validator')
const {Auth} = require('../../../middlewares/auth')
const {Flow} = require('../../models/flow')
const {Art} = require('../../models/art')
const {Favor} = require('../../models/favor')

const router = new Router({
  prefix: '/v1/classic'
})

// 获取最新一期
router.get('/latest', new Auth().m, async (ctx, next) => {
  // 排序 1,2,3,4,5...max  ASC
  // max,... 5, 4, 3, 2, 1    DESC:倒序
  const flow = await Flow.findOne({
    order: [
      ['index', 'DESC']
    ]
  })

  if (!flow) {
    throw new global.errs.NotFound()
  }
  const art = await Art.getData(flow.art_id, flow.type)

  // 查询用户是否点赞
  const isLike = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
  // 序列化 dataValues
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', isLike)

  // const v = await new PositiveIntegerValidator().validate(ctx)
  ctx.body = art
})

// 获取下一期
router.get('/:index/next', new Auth().m, async (ctx, next) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
    where: {
      index: index + 1
    }
  })
  if (!flow) {
    throw new global.errs.NotFound()
  }

  const art = await Art.getData(flow.art_id, flow.type)
  const likeNext = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)

  art.setDataValue('index', flow.index)
  art.setDataValue('likeStatus', likeNext)
  ctx.body = art
})
// 获取上一期
router.get('/:index/previous', new Auth().m, async (ctx, next) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
    where: {
      index: index - 1
    }
  })
  if (!flow) {
    throw new global.errs.NotFound()
  }

  const art = await Art.getData(flow.art_id, flow.type)
  const likeNext = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)

  art.setDataValue('index', flow.index)
  art.setDataValue('likeStatus', likeNext)
  ctx.body = art
})
// 综合 --> 获取下一期、上一期
router.get('/:index/nextOrPrevious', new Auth().m, async (ctx, next) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: 'index'
  })
  const index = v.get('path.index')
  const status = parseInt(v.get('query.status'))
  const flow = await Flow.findOne({
    where: {
      index: index + status
    }
  })
  if (!flow) {
    throw new global.errs.NotFound()
  }

  const art = await Art.getData(flow.art_id, flow.type)
  const likeNext = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('likeStatus', likeNext)
  ctx.body = art
})

//获取期刊点赞信息 
router.get('/:type/:id/favor', new Auth().m, async ctx => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = v.get('path.type')
  const art = await Art.getData(id, type)
  if (!art) {
    throw new global.errs.NotFound()
  }
  const like = await Favor.userLikeIt(id, type, ctx.auth.uid)
  ctx.body = {
    fav_nums: art.favNums,
    likeStatus: like
  }

})


module.exports= router